﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2024/11/30
 * 时间: 21:36
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace volume
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.pictureBox1.MouseWheel += pictureBox1_MouseWheel;
			pictureBox1.Image=imageList1.Images[0];
			this.Size=new Size(30,30);
			
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			int x=int.Parse(iniHelper.Read("Volume","left",inipath));
			int y=int.Parse(iniHelper.Read("Volume","top",inipath));
			this.Location=new Point(x,y);
			 
			int w=int.Parse(iniHelper.Read("Volume","width",inipath));
			int h=int.Parse(iniHelper.Read("Volume","height",inipath));
			this.Size=new Size(w,h);
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[DllImport("user32.dll")]
		static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo);
		 
		[DllImport("user32.dll")]
		static extern Byte MapVirtualKey(UInt32 uCode, UInt32 uMapType);
		 
		private const byte VK_VOLUME_MUTE = 0xAD;
		private const byte VK_VOLUME_DOWN = 0xAE;
		private const byte VK_VOLUME_UP = 0xAF;
		private const UInt32 KEYEVENTF_EXTENDEDKEY = 0x0001;
		private const UInt32 KEYEVENTF_KEYUP = 0x0002;
		 
		/// <summary>
		/// 改变系统音量大小，增加
		/// </summary>
		public void VolumeUp()
		{
		    keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);
		    keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
		}
		 
		/// <summary>
		/// 改变系统音量大小，减小
		/// </summary>
		public void VolumeDown()
		{
		    keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY, 0);
		    keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
		}
		 
		/// <summary>
		/// 改变系统音量大小，静音
		/// </summary>
		public void Mute()
		{
		    keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY, 0);
		    keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
		}
		int n=0;
		void PictureBox1Click(object sender, EventArgs e)
		{
			if (n%2!=1) {
				pictureBox1.Image=imageList1.Images[0];
			}else{
				pictureBox1.Image=imageList1.Images[1];
			}
			n++;
	        Mute();
		}
		private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
		{
			//当e.Delta > 0时鼠标滚轮是向上滚动，e.Delta < 0时鼠标滚轮向下滚动
			if (e.Delta > 0)//滚轮向上
			{
				VolumeUp(); //放大
				//MessageBox.Show("鼠标向上滑动");
			}
			else
			{
				VolumeDown();//缩小
				//MessageBox.Show("鼠标向下滑动");
			} 
		}
		void Timer1Tick(object sender, EventArgs e)
		{
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			string color=iniHelper.Read("Topbar","backcolor",inipath);
			this.BackColor =ColorTranslator.FromHtml(color); 
		}
	}
}
